import { _decorator, Label, math, Node, tween, Tween, v2, v3, Vec2, Widget } from 'cc';
import { UI } from '../enum/UI';
import { Global } from '../Global';
import { AdMgr } from '../manager/AdMgr';
import { AudioMgr } from '../manager/AudioMgr';
import { CoinMgr } from '../manager/CoinMgr';
import { GameMgr } from '../manager/GameMgr';
import { TimeMgr } from '../manager/TimeMgr';
import { TransMgr } from '../manager/TransMgr';
import { InputBlock } from '../misc/InputBlock';
import { UIBase } from '../scriptBase/UIBase';
import { CoinItem } from '../uiItem/CoinItem';
import { ArrayUtil } from '../util/ArrayUtil';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UIWin')
@requireComponent(Widget)
export class UIWin extends UIBase {
    private pointer: Node = null
    private lbCoin: Label = null
    private coinItem: CoinItem = null

    private coin: number = 0
    public set Coin(v: number) {
        this.coin = Math.max(v, 0)
        this.lbCoin.string = `x${this.coin}`
    }

    private multi: number = 1

    private readonly posArr: Vec2[] = [
        v2(-120, -74),
        v2(-72, 80),
        v2(-17, 29),
        v2(32, 86),
        v2(90, 120),
    ]

    private readonly multiArr: number[] = [2, 3, 5, 3, 4]

    private tw: Tween<Node> = null

    // protected onLoad(): void {
    //     this.pointer = this.findNode('Pointer')
    //     this.lbCoin = this.findComp('LbCoin', Label)
    //     this.coinItem = this.findComp('CoinItem', CoinItem)
    // }

    public onOpen(data?: unknown): void {
        AudioMgr.stopBgm()
        AudioMgr.playSfx('通关成功')

        GameMgr.passGame()

        this.coinItem.init(CoinMgr.CurCoin)

        GameMgr.Pause = true

        const duration: number = 0.75
        this.tw = tween(this.pointer).sequence(
            tween(this.pointer).to(duration, { position: v3(120, -25) }),
            tween(this.pointer).to(duration, { position: v3(-120, -25) })
        ).repeatForever().start()

        this.Coin = Global.Normal_Level_Coin_Default_Cnt + (GameMgr.CurLevel - 1) * Global.Normal_Level_Coin_Add_Cnt
        CoinMgr.CurCoin += this.coin

    }

    public onClose(data?: unknown): void {
        GameMgr.Pause = false
        InputBlock.Active = false
    }

    protected async onBtnMultiClick() {
        const onSucc = async () => {
            this.tw.stop()
            const idx: number = ArrayUtil.pickItem([0, 0, 0, 0, 0, 1, 2, 3])
            this.multi = this.multiArr[idx]
            const posRange: Vec2 = this.posArr[idx]
            const posX: number = math.randomRangeInt(posRange.x, posRange.y)
            this.pointer.setPosition(posX, this.pointer.position.y)

            const extraCoin: number = (this.multi - 1) * this.coin
            this.Coin = this.multi * this.coin

            CoinMgr.CurCoin += extraCoin
            this.coinItem.updateCoin(CoinMgr.CurCoin)

            InputBlock.Active = true
            await TimeMgr.delay(2)
            await TransMgr.Inst.fadeIn()
            GameMgr.startGame()
            this.close()
            TransMgr.Inst.fadeOut()
        }

        AdMgr.showRewardedVideo(onSucc)
    }

    protected async onBtnNextClick() {
        this.coinItem.updateCoin(CoinMgr.CurCoin)

        InputBlock.Active = true
        await TimeMgr.delay(2)
        await TransMgr.Inst.fadeIn()
        GameMgr.startGame()
        this.close()
        TransMgr.Inst.fadeOut()
    }

    protected async onBtnCloseClick() {
        await TransMgr.Inst.fadeIn()
        this.close()
        GameMgr.quitGame()
        this.open(UI.Main)
        TransMgr.Inst.fadeOut()
    }

}


